package db.operation;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class jdbcUtils {

	private static Properties config=new Properties();

	static{
		

		try {
			config.load(jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"));
			Class.forName(config.getProperty("driver"));
			
		} catch (Exception e) {
			/////
			throw new ExceptionInInitializerError();
		}
	}

	public static  Connection getConnection() throws SQLException{
		
		return 	DriverManager.getConnection(config.getProperty("url"), config.getProperty("username"), config.getProperty("password"));
	}

	public static void release(Connection connection,Statement st,ResultSet rs) {
		//6.释放资源（释放链接）
		if(rs!=null)
			try {
				rs.close();

			} catch (Exception e) {
				e.printStackTrace();
			}

		if (st!=null) {
			try {
				st.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if(connection!=null){

			try {
				connection.close();
				System.out.println("数据库关闭成功");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


	}

}
